<?php

namespace web\controllers;

use web\models\Account\LoginModel;
use web\models\Account\SignupModel;
use common\biz\AccountBiz;

/**
 * 控制器-账号
 *
 * @author peng.huang
 */
class AccountController extends \web\controllers\base\PageBaseController {

    /**
     * 登录
     * 
     * @return array
     */
    public function actionLogin() {
        $this->title = '用户登录';

        $login = new LoginModel();
        $login->utype = 0; // 手机号
        if ($this->request->isPost() && $login->bind($this->request->post())) {
            $biz = new AccountBiz();
            if ($biz->login($login)) {
                $this->writeAccount(array('aid' => $login->aid,
                    'loginname' => $login->loginname,
                    'loginAt' => time()));
                $this->response->redirect('/site');
            }
        }

        return array('model' => $login);
    }

    /**
     * 注册
     * 
     * @return array
     */
    public function actionSignup() {
        $this->title = '用户注册';

        $signup = new SignupModel();
        $signup->rtype = 0; // 司机
        $signup->utype = 0; // 手机号
        if ($this->request->isPost() && $signup->bind($this->request->post()) && $signup->validate()) {
            $biz = new AccountBiz();
            if ($biz->signup($signup, FALSE)) {
                $this->writeAccount(array('aid' => $signup->aid,
                    'loginname' => $signup->loginname,
                    'loginAt' => time()));
                $this->response->redirect('/site');
            }

            var_dump($signup->getErrors());
        }

        return array('model' => $signup);
    }

    /**
     * 退出
     */
    public function actionLoginout() {
        $this->clearAccount();
        $this->response->redirect('/site');
    }

}
